﻿using System;

using Ah.Testudin.Engine.Actors.Spawning;
using Ah.Testudin.Engine.Events;
using Ah.Testudin.Engine.Events.Components;

using Microsoft.Xna.Framework;

namespace Ah.Testudin.Engine.Actors.Components.Animation
{
    public class FireMissileAnimationComponent : ActorComponentBase, IAnimationComponent
    {
        #region Constructors

        public FireMissileAnimationComponent(
            Actor owner,
            IEventManager eventManager)
            : base(owner, eventManager)
        {
            this.Status = AnimationStatus.Running;

            _eventManager.TriggerEvent(
                new AnimationComponentCreatedEvent(
                    this));
        }

        #endregion

        #region IActorComponent

        public override void Update(GameTime gameTime)
        {
            Matrix playerTransform = this.Owner.GetComponent<TransformComponent>().Transform;

            Matrix missileTransform = playerTransform;

            missileTransform *= 
                Matrix.CreateFromAxisAngle(
                    Vector3.Normalize(playerTransform.Right),
                    -MathHelper.PiOver4);

            missileTransform.Translation = playerTransform.Translation;

            missileTransform *= Matrix.CreateTranslation(
                new Vector3(
                    0,
                    1.8f,
                    0));

            _eventManager.QueueEvent(
                new ActorSpawningEvent(
                    ActorType.Missile,
                    missileTransform,
                    this.Owner,
                    missileTransform.Up * 2.5f));

            this.Status = AnimationStatus.Finished;
        }

        #endregion

        #region IAnimationComponent

        public AnimationStatus Status { get; set; }

        #endregion
    }
}
